package com.ruoyi.web.controller.publish;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.publish.domain.IdAcceptRegister;
import com.ruoyi.publish.domain.IdReport;
import com.ruoyi.publish.service.IIdAcceptRegisterService;
import com.ruoyi.publish.service.IIdReportService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: ${description}
 * @program: ruoyi
 * @author: name
 * @date: 2021-05-08 11:36
 **/
@Controller
@RequestMapping("/publish/paperwork")
public class IdPaperworkController extends BaseController {
    protected final Logger logger = LoggerFactory.getLogger(IdPaperworkController.class);

    private String prefix = "publish/paperwork";

    @Autowired
    private IIdAcceptRegisterService idAcceptRegisterService;

    @Autowired
    private IIdReportService idReportService;


    @GetMapping()
    public String paperwork(){

        return  prefix+"/paperwork";
    }

    /**
     * 查询鉴定受理登记列表
     */
//    @RequiresPermissions("publish:register:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IdAcceptRegister idAcceptRegister) {
        startPage();
        List<IdAcceptRegister> listAcceptRegister = idAcceptRegisterService.selectIdAcceptRegisterList(idAcceptRegister);
        return getDataTable(listAcceptRegister);
    }

    @GetMapping("/detail")
    public String detail(@RequestParam("id") String id, ModelMap mmap){
        IdAcceptRegister idAcceptRegister = idAcceptRegisterService.selectIdAcceptRegisterById(Long.valueOf(id));
        List<IdReport> idReports = idReportService.selectRecordByRegisterId(Long.valueOf(id));
        List<String>  list=new ArrayList<String>();
        for (int i = 0;i < idReports.size();i++){
            if(idReports.get(i).getPublishOps()!=null){
                list.add(idReports.get(i).getPublishOps());
            }
        }
        String publishOps = String.join(",",list);
        mmap.put("idAcceptRegister",idAcceptRegister);
        mmap.put("publishOps",publishOps);
        return prefix+"/detail";
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap){
        IdAcceptRegister idAcceptRegister = idAcceptRegisterService.selectIdAcceptRegisterById(id);
        mmap.put("idAcceptRegister",idAcceptRegister);
        return prefix + "/edit";
    }

    /**
     * 修改终版文书信息
     * @param idAcceptRegister
     * @return
     */
    @Log(title = "编辑文书信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IdAcceptRegister idAcceptRegister){

        return toAjax(idAcceptRegisterService.updateIdAcceptRegister(idAcceptRegister));
    }

    /**
     * 查看案件相关的鉴定结论
     * @param registerId
     * @param mmap
     * @return
     */
    @GetMapping("/reportByRegister")
    public String reportByRegister(Long registerId, ModelMap mmap){
        if (registerId != null) {
            IdAcceptRegister idAcceptRegister = idAcceptRegisterService.selectIdAcceptRegisterById(registerId);
            System.out.println(idAcceptRegister);
            if (idAcceptRegister != null) {
                mmap.put("registerId", registerId);
            }else{
                mmap.put("registerId", 0);

            }
            return prefix + "/reportByRegister";
        }
        return "error";
    }

    /**
     * 查看案件相关的鉴定结论列表
     * @param idReport
     * @return
     */
    @PostMapping("/idReportListByRegisterId")
    @ResponseBody
    public TableDataInfo idReportListByRegisterId(IdReport idReport){
        startPage();
        List<IdReport> list = idReportService.selectIdReportList(idReport);
        return getDataTable(list);
    }

//    /**
//     * 新增终版文书信息
//     */
//    @GetMapping("/add")
//    public String add() {
//        return prefix + "/add";
//    }
//
//    /**
//     * 保存终版文书信息
//     * @param idPaperwork
//     * @return
//     */
//    @Log(title = "文书信息", businessType = BusinessType.INSERT)
//    @PostMapping("/add")
//    @ResponseBody
//    public AjaxResult addSave(IdPaperwork idPaperwork){
//
//        return  toAjax(idPaperworkService.insertIdPaperwork(idPaperwork));
//    }
//
//    @GetMapping("/edit/{id}")
//    public String edit(@PathVariable("id") Long id, ModelMap mmap){
//        IdPaperwork idPaperwork = idPaperworkService.selectIdPaperworkById(id);
//        mmap.put("idPaperwork",idPaperwork);
//        return prefix + "/edit";
//    }
//
//    /**
//     * 修改终版文书信息
//     * @param idPaperwork
//     * @return
//     */
//    @Log(title = "文书信息", businessType = BusinessType.UPDATE)
//    @PostMapping("/edit")
//    @ResponseBody
//    public AjaxResult editSave(IdPaperwork idPaperwork){
//
//        return toAjax(idPaperworkService.updateIdPaperwork(idPaperwork));
//    }
}
